﻿using SK3000RunningTestTool.SKServerDelegate;
using SK3000RunningTestTool.SKServerEnum;
using SK3000RunningTestTool.SKService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;

namespace SK3000RunningTestTool
{
    /// <summary>
    /// 时刻服务器
    /// </summary>
    public class SKServer
    {


        public int ServerID
        {
            get;
            set;
        }

        public string ServerName
        {
            get;
            set;
        }

        public string ServerAddress
        {
            get;
            set;
        }
        private Enum_ServerStatus serverStatus;

        /// <summary>
        /// 服务器状态
        /// </summary>
        public Enum_ServerStatus ServerStatus
        {
            get { return serverStatus; }
            set
            {
                if (serverStatus != value)
                {
                    serverStatus = value;
                    ServerStatusChanged(null);
                }
            }
        }


        public event ServerStatusChangedDelegate ServerStatusChangedEvent;

        public void ServerStatusChanged(object ServerStatusChangedEventValue)
        {
            if (ServerStatusChangedEvent != null)
            {
                ServerStatusChangedEvent(this, ServerStatusChangedEventValue);
            }
        }

        public object Tag
        {
            get;
            set;
        }

        /// <summary>
        /// 服务列表
        /// </summary>
        public List<SKService.ISKService> ServiceList
        {
            get;
            set;
        }

        public Service_MonitorService monServer;

        public UInt16 port = 0;
        /// <summary>
        /// 主动刷新服务器状态
        /// </summary>
        public void ServerStatusRefreshAsync()
        {
            if (monServer != null)
            {
                MonitorServerStatusRedresh();
            }
            else
            {
                //todo:ping 服务器地址
            }
        }


        public void MonitorServerStatusRedresh()
        {
            monServer.MonitionService_ReceiveServerStatausEvent += MonServer_MonitionService_ReceiveServerStatausEvent;
            monServer.ServiceTestAsync(null);
        }

        private void MonServer_MonitionService_ReceiveServerStatausEvent(object sender, SKServerDelegate.Value.MonitionService_ReceiveServerDataValue MonitionService_ReceiveServerDataValue)
        {
            if (MonitionService_ReceiveServerDataValue.ServerStatus == 1)
            {
                ServerStatus = Enum_ServerStatus.InService;
            }
            else if (MonitionService_ReceiveServerDataValue.ServerStatus == 0)
            {
                ServerStatus = Enum_ServerStatus.StandBy;
            }
            else
            {
                ServerStatus = Enum_ServerStatus.Unknown;
            }
        }
    }
}
